package com.yugao.fintech.vctgone.web.util;

import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.net.URLEncoder;

public class FileUtil {
    /**
     * 导出数据
     * @param response 响应
     * @param fileName 文件名
     * @param suffix 文件名后缀
     * @param runnable 导出逻辑
     * @throws IOException /
     */
    public static void export(HttpServletResponse response, String fileName, String suffix, Runnable runnable) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + suffix);

        runnable.run();
    }
}